package com.jxlc.framework.oauth2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxlc.common.exception.GlobalException;
import com.jxlc.common.utils.StringUtils;
import com.jxlc.framework.oauth2.domain.OAuth2Client;
import com.jxlc.framework.oauth2.mapper.FieldOAuth2ClientMapper;
import com.jxlc.framework.oauth2.service.FieldOAuth2ClientService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * OAuth2.0 Token Service 实现类
 *
 */
@Service
public class FieldOAuth2ClientServiceImpl extends ServiceImpl<FieldOAuth2ClientMapper, OAuth2Client>
		implements FieldOAuth2ClientService {

	@Override
	public OAuth2Client getOne(String clientCode, String secret) {
		List<OAuth2Client> list = this.list(new LambdaQueryWrapper<OAuth2Client>()
				.eq(OAuth2Client::getClientCode, clientCode)
				.eq(OAuth2Client::getSecret, secret));
		if (list.isEmpty()) {
			return null;
		}
		if (list.size() != 1) {
			throw new GlobalException("client expected one, but found more than one ");
		}
		return list.get(0);
	}

	@Override
	public List<OAuth2Client> getList(String clientCode) {
		return this.list(new LambdaQueryWrapper<OAuth2Client>()
				.like(StringUtils.isNotEmpty(clientCode), OAuth2Client::getClientCode, clientCode)
				.orderByDesc(OAuth2Client::getClientId));
	}
}
